using System.Linq;
using NUnit.Framework;
using Tycho.Parser.Peg;

namespace Tycho.Parser.Tests.Peg {
    [TestFixture]
    public class ImmutableStackSpecification {
        [Test]
        public void ShouldPushItem () {
            var stack = new ImmutableStack<object> ();

            Assert.True (stack.IsEmpty);

            stack.Push (5);
            Assert.False (stack.IsEmpty);

            Assert.That (stack.Pop (), Is.EqualTo (5));
            Assert.True (stack.IsEmpty);
        }

        [Test]
        public void ShouldEnumerateFromTop () {
            var stack = new ImmutableStack<object> ();
            stack.Push (1);
            stack.Push (2);
            stack.Push (3);
            stack.Push (4);

            var fourItems = stack.CurrentItems;

            stack.Push (5);
            stack.Push (6);

            var sixItems = stack.CurrentItems;

            stack.Pop ();
            stack.Pop ();
            stack.Pop ();
            stack.Push (8);
            stack.Push (9);

            var fiveItems = stack.CurrentItems;

            Assert.That (fourItems.SequenceEqual (new object [] {4, 3, 2, 1}));
            Assert.That (sixItems.SequenceEqual (new object [] {6, 5, 4, 3, 2, 1}));
            Assert.That (fiveItems.SequenceEqual (new object [] {9, 8, 3, 2, 1}));

            Assert.That (stack.SequenceEqual (new object [] {9, 8, 3, 2, 1}));
        }
    }
}